package com.ws.game.lesson3.fire
{
	import com.ws.game.common.framework.BaseCtrl;
	import com.ws.game.common.framework.BaseGameObject;
	import com.ws.game.common.global.Global;
	import com.ws.game.common.utils.MoveDirectionType;
	import com.ws.log.WSLog;
	public class MonsterCtrl extends BaseCtrl
	{		
		private var _shootSpeed:uint =2;
		protected var _lastShoot:Date;
		
		public function MonsterCtrl()
		{
			super();
			_lastShoot = new Date();
		}
		
		override public function set target(value:BaseGameObject):void{
			super.target = value;
			changeDir();
		} 
		
		private function changeDir():void
		{
			var me:ActionObject = _target as ActionObject;
			me.direction = 1+int(Math.random() * 4);
			me.isWalk = true;
		}
		
		override public function autoRun():void{
			var me:Monster = _target as Monster;
			if (!me.nextCanMove) 
				changeDir();
			//auto shoot
			var now:Date = new Date();
			if(Global.basic !=null && (Math.abs(me.x-Global.basic.x)<10 || Math.abs(me.y-Global.basic.y)<10))
			{
				if(Math.random()>0.6) 
					return;
				if(now.time - _lastShoot.time<_shootSpeed*1000) 
					return;
				var tempDir:uint = me.direction; // 临时记录方向
				// 转向基地
				var r:Number = Math.atan2(me.y-Global.basic.y,me.x-Global.basic.x);
				var a:int = r/Math.PI*180;
				
				if(a>80 && a<100)
				{
					me.direction = MoveDirectionType.UP;
				}else if(a>170 && a<190){
					me.direction = MoveDirectionType.RIGHT;
				}else if(a>260 || a<-80){
					me.direction = MoveDirectionType.DOWN;
				}else{
					me.direction = MoveDirectionType.LEFT;
				}
				me.shoot();
				_lastShoot = now;
				me.direction = tempDir;
			}			
		}
		
		override public function dispose():void{
			_target = null;
		}
	}
}